import request from '../../utils/http.js'
const app = getApp()

Page({
  data: {
    verIndex: 1,
    currentIndex: 0,
    winHeight: "", //窗口高度
    currentTab: 0, //预设当前项的值
    MenuButtonTop: 50,
    MenuButtonHeight: 30,
    CustomBar: 60,
    swiperHeight: 0,
    scanTab: 0,
    lunBolistLength: 0,
    lunbolist: [],
    scrollLeft: 0, //tab标题的滚动条位置
    left: 0,
    IsLogin: false,
    personId: 0,
    dianPuId: 0, //店铺id
    tjr_id: 0,
    fenZuList: [],
    gouWuCheCount: 0, //购物车产品数量
    versionStatus: 1, //状态：1正常，2审核',
    mingCheng: '',
    logoImg: '',
    diZhi: '',
    dianPuId: 0,
    dianPuLeiXing: '',
    dianPuLeiXingStr: '',
    dianPuBianHao: '',
    hideNotice: true,
    notice: '',
    marqueePace: 0.1, //滚动速度
    marqueeDistance: 10, //初始滚动距离
    size: 12,
    interval: 4,
    // interval: 30, 
    countTime: '',
    GGao: '',
    searchFlag: false,//是否显示查询商品
    searchList: [],//查询商品list
    searchCount: 0,
    searchStr: '',
    categoryId: ''
  },

  onLoad: function (options) {
    var that = this;
    that.setData({
      CustomBar: app.globalData.CustomBar,
      MenuButtonTop: app.globalData.MenuButtonTop,
      MenuButtonHeight: app.globalData.MenuButtonHeight,
      IsLogin: wx.getStorageSync(app.globalData.LOGINSTATUS),
      personId: wx.getStorageSync(app.globalData.PERSON_ID),
      tjr_id: wx.getStorageSync(app.globalData.TJR_ID),
      dianPuId: wx.getStorageSync(app.globalData.SHOPID),
      verIndex: wx.getStorageSync(app.globalData.VerIndex)
    })

    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        // console.log("屏幕:" + JSON.stringify(res))
        that.setData({
          // winHeight: res.windowHeight - (230 / 750 * res.windowWidth),
          winHeight: res.screenHeight - that.data.CustomBar * 2,
          width: res.screenWidth,
          swiperHeight: res.screenHeight - that.data.CustomBar - 40,
          IsLogin: wx.getStorageSync(app.globalData.LOGINSTATUS),
          personId: wx.getStorageSync(app.globalData.PERSON_ID),
          tjr_id: wx.getStorageSync(app.globalData.TJR_ID),
        });
      }
    });
    that.getAllPro();
  },

  onShow: function () {
    this.setData({
      categoryId: wx.getStorageSync('categoryId')
    })
    this.setCurrentTab()
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }

    let dianPuIdAPP = wx.getStorageSync(app.globalData.SHOPID)
    // proIsChange,是在店铺上下架产品的时候变化
    if (this.data.dianPuId != dianPuIdAPP || app.globalData.proIsChange == 'true') { //切换了店铺，或者修改了产品

      this.setData({
        dianPuId: dianPuIdAPP
      })
      app.globalData.proIsChange = "false"
      this.getAllPro();
    }
    if (this.data.IsLogin) {
      this.getGWCList()
    }
  },

  //获取店铺信息
  getShoppingInfo: function () {

    var that = this

    let data = {
      dianPuId: that.data.dianPuId
    }
    request.get('dianPu/basicInfo', data, res => {
      console.log("店铺信息:" + JSON.stringify(res.data))
      that.setData({
        mingCheng: res.data.mingCheng,
        logoImg: res.data.logo,
        diZhi: res.data.diZhi,
        dianPuId: res.data.dianPuId,
        dianPuLeiXing: res.data.dianPuLeiXing,
        dianPuLeiXingStr: res.data.dianPuLeiXingStr,
        dianPuBianHao: res.data.dianPuBianHao,
      })

      wx.setStorageSync(app.globalData.SHOPID, res.data.dianPuId)
      wx.setStorageSync(app.globalData.ShopName, res.data.mingCheng);
      wx.setStorageSync(app.globalData.ShopLogo, res.data.logo)
      wx.setStorageSync(app.globalData.JKQName, res.data.jianKangQuanMingCheng)

    }, err => {
      wx.showToast({
        title: '获取信息失败', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })

  },


  //清除查询
  bindclean() {

    this.setData({
      searchFlag: false,
      searchStr: ""
    })
  },

  firmSearchInput(e) {
    var searchStr = e.detail.value.trim()
    var that = this

    if (searchStr == '') {
      that.setData({
        searchFlag: false,
        searchList: [],//查询商品list
        searchCount: 0,
      })

    } else {

      wx.showLoading({
        title: '搜索中...',
        mask: true
      })

      let data = {
        dianPuId: that.data.dianPuId,
        productName: searchStr
      }
      request.get('product/searchByName', data, res => {
        // console.log("查询产品:" + JSON.stringify(res.data))
        that.setData({
          searchFlag: true,
          searchList: res.data.list,
          searchCount: res.data.count,
        })

        wx.hideLoading()
      }, err => {
        wx.hideLoading()
        wx.showToast({
          title: '获取信息失败', // 提示的内容
          icon: "none", // 图标，默认success
          duration: 1000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })
      })
    }
  },

  //公告
  userAgreement: function () {
    var that = this
    let data = {
      leiXing: 2,
    }
    request.get('person/userAgreement', data, res => {
      console.log("公告:" + JSON.stringify(res.data))
      that.setData({
        notice: res.data.message,
        hideNotice: res.data.message == '' ? true : false,
      })
      if (res.data.message != '') {
        var length = that.data.notice.length * that.data.size; //文字长度
        var windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕宽度
        that.setData({
          length,
          windowWidth,
          marqueeDistance: windowWidth
        });
        // that.run1();
      }

    }, err => {
      wx.showToast({
        title: '获取信息失败', // 提示的内容
        icon: "none", // 图标，默认success
        duration: 1000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    })

  },

  //滚动公告
  run1: function () {
    var that = this;
    that.data.countTime = setInterval(function () {
      if (-that.data.marqueeDistance < that.data.length) {
        that.setData({
          marqueeDistance: that.data.marqueeDistance - that.data.marqueePace,
        });
      } else {
        clearInterval(that.data.countTime);
        that.setData({
          marqueeDistance: that.data.windowWidth
        });
        that.run1();
      }
    }, that.data.interval);
  },


  //获取轮播图
  lunbo: function () {
    var that = this
    wx.request({
      url: app.globalData.base_url + 'jianKangZhiNan/lunBoTu',
      success: function (res) {
        // console.log("轮播图:" + JSON.stringify(res.data))
        that.setData({
          lunbolist: res.data,
          lunBolistLength: res.data == null ? 0 : res.data.length,
        })
      },
    })
  },

  //切换排版样式
  changeScan() {
    this.setData({
      scanTab: this.data.scanTab > 0 ? 0 : 1
    })
  },


  //获取店铺所有产品
  getAllPro: function () {
    wx.showLoading({
      title: '商品加载中...',
      mask: true
    })
    var that = this

    let data = {
      dianPuId: wx.getStorageSync(app.globalData.SHOPID),
      version: app.globalData.version
    }
    request.get('product/productFenZu', data, res => {
      let list = res.data.fenZuList
      that.setData({
        fenZuList: list
      })
      that.setCurrentTab()
      wx.hideLoading()
    }, err => {
      wx.hideLoading()
    })
  },

  setCurrentTab: function() {
    let categoryId = this.data.categoryId
    let list = this.data.fenZuList
    if (categoryId) list.forEach((item, index) => {
      if(categoryId == item.fenZuId) {
        this.setData({
          currentTab: index
        })
      }
    })
  },

  //获取购物车数量
  getGWCList: function () {
    var that = this
    let data = {
      dianPuId: that.data.dianPuId,
      personId: that.data.personId
    }
    request.get('gouWuChe/xiangQing', data, res => {
      var count = 0;
      // console.log("购物车:" + JSON.stringify(res.data))
      if (res.data.list) {
        res.data.list.forEach((item, index) => {
          count = parseInt(count) + parseInt(item.shuLiang)
        })
        that.setData({
          gouWuCheCount: count
        })
      } else {
        that.setData({
          gouWuCheCount: 0
        })
      }
    })
  },

  //轮播图跳转
  toLunboPath: function (e) {
    var path = e.currentTarget.dataset.path;
    console.log(path + '&personId=' + this.data.personId + '&tjr_id=' + this.data.tjr_id)
    wx.navigateTo({
      url: path + '&personId=' + this.data.personId + '&tjr_id=' + this.data.tjr_id,
    })
  },

  //产品详情页
  toProductInfo: function (e) {
    var productId = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.c;
    app.globalData.tabIndex = this.data.currentTab
    if (type == 2) {
      wx.navigateTo({
        url: '/pages/product_info_fwb/product_info_fwb?productId=' + productId + '&personId=' + this.data.personId + '&tjr_id=' + this.data.tjr_id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/product_info/product_info?productId=' + productId + '&personId=' + this.data.personId + '&tjr_id=' + this.data.tjr_id,
      })
    }
  },

  //产品分组切换
  onTabsChange(e) {
    console.log(e)
    this.setData({
      currentTab: e.currentTarget.dataset.index
    })
  },

  //跳转购物车
  bindToGWC: function () {
    wx.navigateTo({
      url: '/pages/gouwc/gouwc',
    })
  },

  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    //  console.log("节点:" + JSON.stringify(e))
    let index = e.detail.current
    if (this.data.currentTab == index) {
      return false;
    } else {
      this.setData({
        currentTab: index
      })
    }
  },

  //轮播广告图
  bindchange(e) {
    this.setData({
      currentIndex: e.detail.current
    })
  }
})